#include "guiterm.hpp"
#ifndef TERMINALEMULATOR_HPP
#define TERMINALEMULATOR_HPP

class TerminalEmulator
	:public CharacterGrid
{
	Q_OBJECT;
public:
	TerminalEmulator(RemoteJob *owner, int width, int height);
	~TerminalEmulator();
	
	void do_cursor(int x, int y, wchar_t *text, int len, unsigned long attr, int lattr);
	void do_text(int x, int y, wchar_t *text, int len, unsigned long attr, int lattr);
	
	void setWidth(int width);
	void setHeight(int height);
	int heightForWidth(int width, bool abridged) const;
	/// The height of this terminal, as reported to the program running in it.
	/// Doesn't account for the fact that unused space at the bottom gets
	/// chopped.
	int getTerminalHeight() const;
	
	void setScrollbar(int total, int start, int page);
	void setScroll(int where);
	
	int getChar(int col, int row) const;
	QColor getForeColor(int col, int row) const;
	QColor getBackColor(int col, int row) const;
	int receiveData(const char *data, int length);
	
	RemoteJob *getOwner();
	
	void finish();
	
protected:
	struct TerminalCell {
		TerminalCell();
		bool operator!=(const TerminalCell &oth);
		
		wchar_t ch;
		long attr;
		long lattr;
	};
	vector< vector<TerminalCell> > cells;
	
	RemoteJob *owner;
	
	int height;
	
	int scrollbarTotal;
	int scrollbarStart;
	int scrollbarPageSize;
	
	/// Space below the bottom-most cell that's been written to isn't shown,
	/// even though it's technically part of the terminal.
	void setAbridgedHeight(int height);
	int calculateAbridgedHeight();
	int abridgedHeight;
	bool abridged;
	
	struct config_tag *terminalConfig;
	struct terminal_tag *terminal;
	struct unicode_data *ucsdata;
};

#endif
